package com.example.demo.controller;

import javax.servlet.http.HttpServletResponse;

import com.example.demo.ServerResponse;
import com.example.demo.Service.IGoodsService;
import com.example.demo.entity.Goods;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * goodsController
 * 
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询goods列表
     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(Goods goods, @RequestParam( defaultValue = "1") int pageNum, @RequestParam( defaultValue = "5") int pageSize)
    {
        PageResult pageResult = goodsService.selectGoodsList(goods,pageNum,pageSize);
        return ServerResponse.createBySuccess(200,pageResult,"查询成功");
    }

    @PassToken
    @GetMapping("/frontList")
    public ServerResponse list1(Goods goods)
    {
        System.out.println(goods.getCid());
         List<Goods> list= goodsService.selectGoodsListNoPage(goods);
        return ServerResponse.createBySuccess(200,list,"查询成功");
    }
    /**
     * 获取goods详细信息
     */
    @PassToken
    @GetMapping(value = "/{gid}")
    public ServerResponse getInfo(@PathVariable("gid") Long gid)
    {
        Goods goods = goodsService.selectGoodsByGid(gid);
        return ServerResponse.createBySuccess(200,goods,"查询成功");
    }

    /**
     * 新增goods
     */
    @PostMapping
    public ServerResponse add(@RequestBody Goods goods)
    {
        int i = goodsService.insertGoods(goods);
        if (i>0){
            return ServerResponse.createBySuccess(200,"","添加成功");
        }else {
            return ServerResponse.createBySuccess(500,"","添加失败");
        }
    }

    /**
     * 修改goods
     */
    @PutMapping
    public ServerResponse edit(@RequestBody Goods goods)
    {
        int i = goodsService.updateGoods(goods);
        if (i>0){
            return ServerResponse.createBySuccess(200,"","更新成功");
        }else {
            return ServerResponse.createBySuccess(500,"","更新失败");
        }
    }

    /**
     * 删除goods
     */
	@DeleteMapping("/{gids}")
    public ServerResponse remove(@PathVariable Long[] gids)
    {
        int i = goodsService.deleteGoodsByGids(gids);
        if (i>0){
            return ServerResponse.createBySuccess(200,"","删除成功");
        }else {
            return ServerResponse.createBySuccess(500,"","删除失败");
        }
    }
}
